package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.domain.UserTravel;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.mapper.UserTravelMapper;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatsCacheService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private UserTravelMapper userTravelMapper;
    @Reference
    private ITravelStatsCacheService travelStatsCacheService;

    public PageInfo<Travel> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public TravelContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Travel selectById(Long id) {
        Travel travel = travelMapper.selectByPrimaryKey(id);
        TravelContent travelContent = contentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    public List<Travel> listViewnumTop3(Long id) {

        List<Travel> travels = travelMapper.selectViewnumTo3ById(id);
        return travels;
    }

    public AjaxResult saveOrUpdate(Travel entity, UserInfo userInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        Date date = new Date();
        //最后更新时间
        entity.setLastUpdateTime(date);
        //填充摘要
        entity.setSummary("AI生成");

        //处理文章内容
        TravelContent content = entity.getTravelContent();
        if (entity.getId() == null){
            //发布时间
            entity.setReleaseTime(date);

            //创建时间
            entity.setCreateTime(date);
            travelMapper.insert(entity);

            //同步保存
            content.setId(entity.getId());
            contentMapper.insert(content);
        }else {
            if (userInfo.getId().equals(entity.getAuthor().getId())) {
                travelMapper.updateByPrimaryKey(entity);
                //同步更新
                content.setId(entity.getId());
                contentMapper.updateByPrimaryKey(content);
            } else {
                ajaxResult.setSuccess(false);
                ajaxResult.setMsg("?搞事情");
            }
        }
        ajaxResult.setData(entity.getId());
        return ajaxResult;
    }

    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override//通过用户id获取用户的所有游记
    public PageInfo<Travel> getUserIdTrave(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list=travelMapper.selectUserIdTravel(qo);
        return new PageInfo<>(list);
    }

    @Override//获取所有游记的阅读数
    public Integer getUserViewnum(Long id) {
        List<Travel> travels = travelMapper.selectAll();
        Integer totalViewnum=0;
        for (Travel t:travels){
            totalViewnum= totalViewnum + t.getViewnum();
        }
        return totalViewnum;
    }

    @Override//获取总评论
    public Integer getUserReplynum(Long id) {
        List<Travel> travels = travelMapper.selectAll();
        Integer totalReplynum=0;
        for (Travel t:travels){
            totalReplynum=totalReplynum+t.getReplynum();
        }
        return totalReplynum;
    }

    @Override
    public boolean iFavor(UserInfo user, Long travelId) {

        List<UserTravel> userTravels = userTravelMapper.selectByUserIdAndTravelId(user.getId(), travelId);

        if(userTravels.size() == 0){
            UserTravel ut = new UserTravel();
            ut.setUserId(user.getId());
            ut.setTravelId(travelId);
            userTravelMapper.insert(ut);
            return true;
        }else{
            userTravelMapper.deleteByUserIdAndTravelId(user.getId(), travelId);
            return false;
        }
    }

    @Override
    public PageInfo<Travel> listByUserFavor(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Travel> list = travelMapper.listByUserFavor(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void deleteIsFavor(UserInfo user, Long travelId) {
        Long id = user.getId();
        userTravelMapper.deleteIsFavor(id,travelId);
    }

    @Override
    public Long selectThumbsupnumTotal(Long userId) {
        Long sum = 0L;
        List<Travel> travels = travelMapper.selectByUserId(userId);
        for (Travel travel : travels) {
            sum += travelStatsCacheService.getTravelStats(travel.getId()).getThumbsupnum();
        }
        return sum;
    }

    @Override
    public void updateState(Travel travel) {
        travelMapper.updateState(travel);
    }
}
